package com.example.capstome;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


public class DB_Manager_Table_Adapter extends RecyclerView.Adapter<DB_Manager_Table_Adapter.DBHelperColumnViewHolder> {
    private Context context;
    private DB_Helper dbHelper;
    private String tableName;

    public DB_Manager_Table_Adapter(Context context, String tableName){
        this.context = context;
        this.tableName = tableName;
        this.dbHelper = new DB_Helper(context);
    }

    @NonNull
    @Override
    public DBHelperColumnViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new DBHelperColumnViewHolder(LayoutInflater.from(context).inflate(R.layout.db_manager_table_item, parent, false));
    }

    @SuppressLint("Range")
    @Override
    public void onBindViewHolder(@NonNull DBHelperColumnViewHolder holder, int position) {
        switch (tableName){
            case DB_Common_Contract.CommentEntry.TABLE_NAME:{
                if (position == 0){
                    holder.column1.setText(DB_Common_Contract.CommentEntry._ID);
                    holder.column2.setText(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_UserID);
                    holder.column3.setText(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_TEXT);
                    holder.column4.setText(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_SUPPORT);
                    holder.column5.setText(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_TIME);
                }
                else {
                    String  query_comment = "SELECT * FROM " + DB_Common_Contract.CommentEntry.TABLE_NAME +
                            " ORDER BY " + DB_Common_Contract.CommentEntry.COLUMN_COMMENT_SUPPORT + " DESC;";
                    SQLiteDatabase db_count = dbHelper.getReadableDatabase();
                    Cursor cursor = db_count.rawQuery(query_comment, null);
                    if (cursor != null && cursor.moveToPosition(position-1)) {
                        holder.column1.setText(cursor.getString(cursor.getColumnIndex(DB_Common_Contract.CommentEntry._ID)));
                        holder.column2.setText(cursor.getString(cursor.getColumnIndex(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_UserID)));
                        holder.column3.setText(cursor.getString(cursor.getColumnIndex(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_TEXT)));
                        holder.column4.setText(cursor.getString(cursor.getColumnIndex(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_SUPPORT)));
                        holder.column5.setText(cursor.getString(cursor.getColumnIndex(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_TIME)));
                    }
                }
                break;
            }
            case DB_User_Contract.UserEntry.TABLE_NAME:{
                if (position == 0){
                    holder.column1.setText(DB_User_Contract.UserEntry._ID);
                    holder.column2.setText(DB_User_Contract.UserEntry.COLUMN_USER_NAME);
                    holder.column3.setText(DB_User_Contract.UserEntry.COLUMN_USER_ImgURL);
                }
                else {
                    String countQuery = "SELECT * FROM " + tableName ;
                    SQLiteDatabase db_count = dbHelper.getReadableDatabase();
                    Cursor cursor = db_count.rawQuery(countQuery, null);
                    if (cursor != null && cursor.moveToPosition(position-1)) {
                        holder.column1.setText(cursor.getString(cursor.getColumnIndex(DB_User_Contract.UserEntry._ID)));
                        holder.column2.setText(cursor.getString(cursor.getColumnIndex(DB_User_Contract.UserEntry.COLUMN_USER_NAME)));
                        holder.column3.setText(cursor.getString(cursor.getColumnIndex(DB_User_Contract.UserEntry.COLUMN_USER_ImgURL)));
                    }
                }
                break;
            }
            default:{
                // 出错信息
            }
        }
    }

    @Override
    public int getItemCount() {
        int num = 0;
        // 查询数据库中评论总数
        String countQuery = "SELECT COUNT(*) FROM " + tableName;
        SQLiteDatabase db_count = dbHelper.getReadableDatabase();
        Cursor cursor_count = db_count.rawQuery(countQuery, null);
        if (cursor_count.moveToFirst()) {
            num =  cursor_count.getInt(0);
        }
        cursor_count.close();
        return num + 1;
    }

    public class DBHelperColumnViewHolder extends RecyclerView.ViewHolder{
        TextView column1;
        TextView column2;
        TextView column3;
        TextView column4;
        TextView column5;
        TextView column6;
        TextView column7;
        TextView column8;


        public DBHelperColumnViewHolder(@NonNull View itemView) {
            super(itemView);
            column1 = itemView.findViewById(R.id.column1);
            column2 = itemView.findViewById(R.id.column2);
            column3 = itemView.findViewById(R.id.column3);
            column4 = itemView.findViewById(R.id.column4);
            column5 = itemView.findViewById(R.id.column5);
            column6 = itemView.findViewById(R.id.column6);
            column7 = itemView.findViewById(R.id.column7);
            column8 = itemView.findViewById(R.id.column8);
        }
    }
}
